Method for decoding sequences of guest instructions for a host computer

ABSTRACT

Emulator performance can be improved by recognizing repeated sequences of the same instruction, or commonly groups of instructions. For example, it is very common to see a three instruction sequence of MOVEM, UNLK A6, and RTS instructions for a 68020 processor in procedure exit code. By looking for these sequences, and combining the operations performed by the separate sequences, overhead of decoding and dispatching the individual instructions in the sequence can be eliminated, and performance improved. Common instruction sequences or repeated sequences in a guest program are detected during emulation of the guest program on a host processor, and performance of the emulation optimized based on the detected sequences. Thus, the emulation logic comprising host instructions embedded within a particular emulation program for a particular guest instruction, detects a particular sequence of guest instructions and in response to detection of the particular sequence bypasses the dispatch logic for guest instructions within the particular sequence. The sequences detected can comprise repeated guest instructions, or common sequences of two or more than two guest instructions.

This application is a Continuation of Ser. No. 08/287,715, filed Aug. 9,1994, now abandoned, which is a Continuation of Ser. No. 08/059,215,filed May 7, 1993, now abandoned.

BACKGROUND OF THE INVENTION

1. Field of the Invention

The present invention relates to the emulation of software written for agiven computer on a different computer which executes a different set ofinstructions; and more particularly to a system for decoding guestinstructions into host instructions by the host computer.

2. Description of the Related Art

Central processing units for computers are designed to execute aspecific set of instructions unique to a particular central processingunit. Thus, a microprocessor in one family, such as the Motorola 68000family, executes software written in a language unique to the 68000family, while processors in the Intel 80286 family execute softwarewritten with another language which is unique to that family ofprocessors. A need often arises to execute software written for aparticular processor in a host processor that utilizes a differentlanguage. For the purposes of this application, the language for thehost CPU is based on "host instructions", while the language for otherCPUs are referred to as "guest instructions".

Because of the large body of software written for existing processors,such as the Motorola 68000 series, new processors which are designedoften attempt to emulate the 68000 series processors in software. Thisemulation process involves first decoding the 68000 series guestinstructions into a sequence of host instructions which accomplish theresult intended by the guest instruction. The routines needed to emulatea given instruction are stored in a host addressable table. Forinstance, in one prior art system, each guest instruction is used togenerate a jump table pointer which points to a table that includes oneentry for each of the possible combinations of operational code andaddressing mode for a guest instruction. The jump table stores a pointerto the particular code segment adapted to a particular code combination.See, ARRANGEMENT FOR SOFTWARE EMULATION, International Application No.PCT/GB87/00202; invented by MacGregor.

A disadvantage of this prior art technique arises because of the delayinvolved in composing a jump table pointer based on the guestinstruction, looking up a second pointer in the jump table, and thenaccessing the emulation segment. Because of data access latencies andthe like, this can significantly slow down the emulation routine.

At least one prior art system based upon a jump table includes anability to detect common opcode sequences, so that optimized emulationprograms may be used for such sequences. This prior art system is basedon eight bit opcodes, and forms a 16 bit index using the two 8 bitopcodes in the sequence. Using the 16 bit index, a jump table entry canbe found to an optimized emulation program. This approach works wellwith eight bit opcodes, which result in an index of manageable size.However for current 16 bit opcode systems, this approach would require a32 bit index--or with over 4 billion possible entries. The prior art hasnot provided a practical technique for optimizing the emulation ofcommonly occurring guest instruction sequences for longer opcodes.

Accordingly, it is desirable to provide an emulation system whichenhances the performance of the host processor in emulation of guestinstructions sequences particularly suited to longer opcodes of currentprocessor architectures.

SUMMARY OF THE INVENTION

Emulator performance can be improved by recognizing repeated sequencesof the same instruction, or commonly used groups of instructions. Forexample, it is very common to see a three instruction sequence of MOVEM,UNLK A6, and RTS instructions for a 68020 processor in procedure exitcode. By looking for these sequences, and combining the operationsperformed by the separate sequences, overhead of decoding anddispatching the individual instructions in the sequence can beeliminated, and performance improved. The present invention provides atechnique in which common instruction sequences or repeated sequences ina guest program can be detected during emulation of the guest program ona host processor, and performance of the emulation optimized based onthe detected sequences. According to the present invention, thisdetection is performed in the execution of the emulation program for thefirst instruction in the sequence. This approach does add some overheadto the general case of the first instruction when it is not followed bythe expected second instruction. However, these optimizations improveperformance if the probability of this sequence occurring is very highin the guest program being emulated.

Accordingly, the present invention can be characterized as a system fordecoding guest instructions in a host processor. The system includes aguest program store in host addressable memory to store at least oneprogram of guest instructions. An emulation program store in the hostaddressable memory includes a set of emulation programs which comprisehost instruction routines for respective particular guest instructions.Further, dispatch logic is coupled with the guest program store and theemulation program store to dispatch emulation programs in response toguest instructions in a guest instruction program, and in response tothe emulation programs in the emulation program store. Emulation logiccomprising host instructions embedded within a particular emulationprogram for a particular guest instruction, detects a particularsequence of guest instructions and in response to detection of theparticular sequence bypasses the dispatch logic for guest instructionswithin the particular sequence. The sequences detected can compriserepeated guest instructions, or common sequences of two or more than twoguest instructions.

According to a further aspect of the invention, both the dispatch logicand the emulation logic which detects guest instruction sequences arebased on host instructions embedded within the emulation programs.

Further, the decoding system is further optimized according to thepresent invention by dividing the emulation program store into adispatch store and an emulation routine store. The dispatch store isplaced in host addressable memory, and stores a set of dispatch entries.Each dispatch entry includes a plurality of host instructions of theemulation program corresponding to a particular guest instruction. Theemulation routine store is placed in host addressable memory andincludes a set of emulation entries beginning at corresponding emulationtable addresses. Each emulation entry in the set includes a hostinstruction routine for the particular emulation program. The pluralityof host instructions in the dispatch entries include a host jumpinstruction which causes a jump upon execution by the host processor toan emulation table address of a corresponding emulation entry in theemulation routine store. The emulation entries include host instructionswhich upon execution by the host processor form an emulation programaddress to a dispatch entry in response to a next guest instruction, andjump directly to the dispatch table address. Thus, according to thisoptimization, emulation programs in the emulation program store comprisea dispatch table entry and a host instruction routine in the emulationroutine store. Further, the dispatch logic and the logic for detectingguest instruction sequences are embedded in the host instruction routinestored in the emulation routine store.

According to this embodiment, the host system includes a guestinstruction pointer store for a guest instruction pointer indicating aguest instruction address, a prefetched guest instruction store for aguest instruction read from the program of guest instructions inresponse to the guest instruction pointer, and an emulation programpointer store for an emulation program address formed in response to theguest instruction read from the prefetched guest instruction store.Also, the emulation program includes a first macro which upon executionby the host processor forms an emulation program address in theemulation program pointer store in response to the guest instruction inthe prefetched guest instruction store. A second macro of hostinstructions reads the next guest instruction from an address indicatedby the guest instruction pointer into the prefetched guest instructionstore. A third macro of host instructions causes a jump to the emulationprogram indicated by the emulation program address in the emulationprogram pointer store. A fourth macro embedded within the emulationroutine bypasses at least the third dispatch segment upon detection ofthe particular sequence of guest instruction.

Accordingly, a decoding technique for emulating a guest program ofinstructions in a host processor is provided. The system includes anoptimized technique for dispatching emulation programs in response toguest instructions, in combination with the ability to detect commonsequences of guest instructions. Upon detection of common sequences,optimized emulation programs can be executed which bypass thedispatching overhead, and which, where appropriate, optimize theemulation of the instruction sequence. The improved emulator optimizescommon instruction sequences in the guest programs being emulated, evenfor guest instructions which comprise long opcodes (e.g., 16 bits).

Other aspects and advantages of the present invention can be seen uponreview of the figures, the detailed description, and the claims whichfollow.

BRIEF DESCRIPTION OF THE FIGURES

FIG. 1 is a schematic block diagram of a computer system implementingthe present invention.

FIG. 2 is a diagram of a dispatch store according to the presentinvention.

FIG. 3 is a diagram of an emulation routine store according to thepresent invention.

FIG. 4 is a flow chart illustrating the decoding method according to thepresent invention.

FIG. 5 illustrates an emulation program according to the presentinvention.

FIG. 6 illustrates an alternative emulation program according to thepresent invention.

FIG. 7 is a flow chart of an emulation program which is optimized for athree opcode sequence, which may be common in guest programs.

FIG. 8 illustrates an emulation program according to the presentinvention for a guest instruction which may be a first instruction of athree instruction sequence of guest instructions.

FIG. 9 illustrates an emulation program according to the presentinvention for a guest instruction, which may be the first guestinstruction in a two opcode sequence.

FIG. 10 illustrates an emulation program according to the presentinvention for a guest instruction, which may be repeated.

DESCRIPTION OF THE PREFERRED EMBODIMENTS

A detailed description of preferred embodiments of the present inventionis provided with respect to FIGS. 1-10.

I. Emulator System

FIG. 1 illustrates a host processor which is adapted to emulate guestinstructions according to the present invention. The host processorincludes a host CPU 10 which executes host instructions. Coupled withthe host CPU 10 are a set of general purpose registers 11, and a set ofspecial purpose registers 12, implemented as commonly known in theindustry as part of an integrated circuit microprocessor, incorporatingthe CPU 10.

The host CPU 10 is coupled to a system bus 13. The bus is also coupledto input devices such as a keyboard and mouse 14, a display system 15,and a memory system 16 such as a disk drive.

The host processor system also includes host addressable memory, whichincludes an emulation program store 17, a sequence of guest instructionsin a guest program store 18, and other memory 19. The emulation programstore 17, according to a preferred embodiment of the present invention,includes an emulation dispatch store 20, and a set of emulation routines21.

FIG. 2 illustrates the structure of a preferred embodiment of theemulation dispatch store 20. The emulation dispatch store 20 includes aset of instruction pairs, e.g., pair 30, pair 31, pair 32. Each pairincludes a host non-jump instruction and a host jump instruction.

According to the preferred embodiment, the dispatch store is an indexedtable of 65536 pairs of host instructions, which correspond to the entrypoints for emulation programs to emulate each of the 65536 possibleguest instruction encodings for a Motorola 68020 microprocessor assemblylanguage. The first instruction of the pair will generally perform someoperation related to a source operand fetching or addressing. The secondinstruction of the pair is generally a branch instruction to anemulation routine which resides outside of the dispatch store in theemulation routine store 21. Since each pair of instructions will occupy8 bytes, the total size of the dispatch store is 512K bytes. Thedispatch store may be located, for instance, at addresses (hex) 68000000through (hex) 6807FFFF.

The alignment of the beginning of the store is important. In a preferredsystem, the store starts at either the beginning of a 2 Megabyte addressboundary, or a 512 Kilobyte address boundary past the 2 Megabyteboundary. By having this 512K byte block aligned onto a 512K byteaddress, block address translation registers can be used to performaddress translation of a fairly randomly accessed block of code andeliminate potential thrashing in translation lookaside buffers.

The 512K byte alignment also allows a single instruction to index thedispatch store using a sixteen bit 68020 opcode multiplied by 8. Thus, asingle host instruction can shift a 68020 opcode left by 3 bits(multiplied by 8), and insert it into the address of the base of thetable to form the address of a dispatch table entry for that opcode.

By having this table start in the first 1 Megabyte of a 2 Megabytealigned boundary, where the second 1 Megabyte of addresses will cause anexception if accessed, it is possible to use an additional address bitto assist in the detection of address errors as described below.

As illustrated in FIG. 2, the host CPU 10 executes a host instruction inorder to dispatch a guest instruction by accessing the first instructionin an instruction pair in the dispatch store. The first instructionexecutes, and then a second instruction in the dispatch pair isexecuted. This second instruction includes a jump instruction which goesto an emulation block in the emulation routine store 21.

FIG. 3 illustrates the implementation of the emulation routine store 21.The emulation routines are allocated to a 64K block of bytes for hostinstruction routines to which the jump instruction in the dispatch entrybranches. In general, the first two host instructions in an emulationprogram reside in an entry in the dispatch store, while the remaininginstructions, which are referred to as emulation routines, reside in theemulation routine block of code. This block may be located, forinstance, at addresses (hex) 68080000 through (hex) 6808FFFF.

As above, the alignment of the block of code for the emulation routinesis also important. In a preferred system, it needs to start at thebeginning with 64K byte boundary.

During emulation, it is frequently desirable to compute the address of aguest instruction within the emulated block, such as by computation of aPC relative address. The host architecture in a preferred system may notprovide an instruction to compute a PC relative address. By storing theaddress of the beginning of the emulation routine block so that it haszeros in the 16 least significant bits in a host register, referred toas code₋₋ ptr, a computation of the address of any label within this 64Kbyte block of code can be optimized by using the value in code₋₋ ptr asa code base by doing an OR immediate, with a 16 bit immediate value asthe offset.

Within the 64K byte block of emulation routine code, there is additionalattention paid to code alignment. In particular, the emulation blocksare aligned into blocks which match the processor caching routing usedto retrieve the code. In a preferred system, a processor cache uses 32byte cache blocks in a cache line of 64 bytes, and the emulation blocksare packed into aligned 32 and 64 byte blocks.

Thus, as illustrated in FIG. 3, the emulation routine store 21 mayinclude a plurality of emulation routines including emulation block 40,emulation block 41, emulation block 42, emulation block 43, emulationblock 44, etc. Each of these blocks 40-44 is either a 32 or 64 byteblock.

A particular emulation block, e.g., block 42, is entered by a jump fromthe dispatch table, and ends with an instruction to dispatch the nextguest instruction.

As illustrated in FIG. 3, some emulation blocks may include effectiveaddress calculation routines, such as block 43. Such effective addressroutines are entered by a jump from the dispatch table as describedbelow, and end with a jump to a return address of an emulation blockwithin the emulation routine memory.

FIG. 4 illustrates the emulation decoding process according to thepresent invention. As mentioned above, the host processor 10 shown inFIG. 1 includes a plurality of special purpose and general purposeregisters. The general purpose register disp₋₋ table stores a pointer toa dispatch table entry. Also, at this point in the decoding sequence, aspecial purpose register labelled ctr will contain the same value asdisp₋₋ table. A general purpose register labelled pc stores a guestinstruction pointer 2 bytes past a current guest instruction. A generalpurpose register labelled ptefetch₋₋ data stores a next guestinstruction, as indicated at block 50. As mentioned above, the emulationroutines include host instructions distributed within the routines tocarry out the decoding process. Thus, each emulation program willinclude a DISPATCH macro, generally 51, which does inter-instructionprocesses which may be required between guest instructions, and causes ajump to the dispatch table entry indicated by the pointer in registerctr. The emulation program also includes a macro referred to as DECODE1macro 52, which takes the next guest instruction from the prefetch₋₋data register, multiplies that instruction by 8, and inserts the resultsin the general purpose register labelled disp₋₋ table so that it formsan address of a dispatch table entry.

A next macro 53 within an emulation program referred to as DECODE2macro, copies the value in the disp₋₋ table register to the specialpurpose register ctr.

A final macro referred to as PREFETCH macro 54 is included within anemulation program. The PREFETCH macro 54 advances the guest instructionpointer in register pc by 2 bytes, then causes a prefetch of the nextguest instruction from the address indicated by the pointer in registerpc and places the prefetched instruction in the general purpose registerprefetch₋₋ data. The final macro in a given emulation routine is theDISPATCH macro 51. Thus, as illustrated in FIG. 4, an emulation programfor a particular guest instruction begins immediately after the jumpinstruction of the DISPATCH macro 51.

The structures of alternative emulation programs, according to thepresent invention, are shown in FIGS. 5 and 6. FIG. 5 illustrates thegeneral case. Line 1 is the first instruction INST1 stored in a dispatchentry in the dispatch store. Generally, this is a non-jump instructionrelevant to addressing mode of the guest instruction. Line 2 stores thesecond instruction INST2 stored in the dispatch entry. Generally, thisis a jump to a block of code in the emulation routine store.

Line 3 corresponds to the instruction or instructions at the beginningof an emulation block. Within an emulation program, the DECODE1 macrooccurs next, as indicated at line 4. After the DECODE1 macro in line 4,an instruction or instructions may be included relevant to emulation ofthe guest instruction. Next, a DECODE2 macro is executed as indicated atline 6. The DECODE2 macro in line 6 may be followed by otherinstructions indicated by line 7, relevant to the decoded guestinstruction. Next in the sequence, a PREFETCH macro of line 8 isexecuted. The PREFETCH macro may be followed by an instruction orinstructions represented by line 9 of the emulation program. Finally,the DISPATCH macro is executed as indicated at line 10. The firstinstruction of the DISPATCH macro is a conditional jump to the value inthe ctr register, if no special conditions are pending.

FIG. 5 illustrates the distribution of the DECODE1, DECODE2, PREFETCH,and DISPATCH macros within an emulation program. It will be appreciatedby those of skill in the art that the presence of instructions betweensuch macros may or may not occur. However, by distributing the macrosamong the instructions of the emulation program, the programmer can takeadvantage of any data or instruction access latency occurring in theprogram to improve performance.

FIG. 6 illustrates an emulation program which is used when a singleinstruction in a dispatch entry in the dispatch table is insufficient tohandle addressing mode issues. In this case, the first instruction, INSTof the dispatch entry, is shown in line 1. This instruction is anon-jump instruction which sets the value of a return address in a hostregister labelled rtn₋₋ addr which is a general purpose register in apreferred embodiment of the present invention. The next instruction inline 2 of FIG. 6 is the instruction INST2 in the dispatch entry. Thisinstruction causes a jump to an effective address routine stored in theemulation routine store, as illustrated at element 43 of FIG. 3.

Line 3 of FIG. 6 illustrates the beginning instructions of the effectiveaddress routine. Line 4 of the program shown in FIG. 6 is an instructionwhich results in moving the return address from the register rtn₋₋ addrto a special purpose register Ir, which is used for jump addresses bythe host processor. This may or may not be necessary in a givenimplementation of the present invention. Line 5 of the programillustrates the final instruction of the effective address routine,which requires a jump to the return address stored in the register Ir.This jump results in execution of the instructions illustrated at line6, starting the emulation block for the guest instruction. Thisemulation block will include the DECODE1 macro, line 7, possibly otherinstructions, line 8, the DECODE2 macro, line 9, the PREFETCH macro,line 10, possibly other instructions, line 11, and the DISPATCH macro,line 12, as described above with respect to FIG. 5.

II. Multi-Instruction Sequence Emulation

The emulation system according to the present invention may be furtheroptimized for sequences of guest instructions which are expected to berelatively common in the guest code to be emulated. For instance, themove multiple opcode MOVEM in the 68020 architecture may be a firstinstruction in a three instruction sequence, including MOVEM, unlinkUNLK, and return-to-subroutine RTS, which commonly occurs in manyprograms written for the 68020 processor. Thus, FIG. 7 illustrates howan emulation program for the MOVEM opcode may be implemented accordingto the present invention.

The MOVEM emulation program, after dispatching, as described above, willinclude a section which begins execution of the opcode, including theDECODE1, DECODE2, and PREFETCH macros (block 100). After theprefetching, the next opcode (2nd) can be tested to determine whether itis the UNLK opcode (block 101). If it is not, then the expected sequenceis not occurring, and the MOVEM instruction is completed (block 102).

If at block 101, the emulation program detects a UNLK instruction, thenthe algorithm tests whether special conditions are pending, such as aninterrupt, or instruction tracing condition (block 103). If a specialcondition is pending, then the algorithm branches back to complete theMOVEM instruction at block 102, because the special condition must behandled between guest instructions.

If no special condition is pending at block 103, then the next opcode(3rd) is tested (block 104). If the third opcode is not the RTSinstruction, then the predicted three instruction sequence is not found,and the algorithm branches to complete a combined MOVEM and UNLKinstruction program (block 105). If the next opcode is found to be RTSin block 104, then the algorithm branches to complete the combinedMOVEM, UNLK, and RTS instruction sequence (block 106).

Thus, it can be seen that for the combined sequence, the overhead of thedecoding and dispatching logic for UNLK and RTS is bypassed.

FIG. 8 illustrates an emulation program for the MOVEM instruction, or asimilar instruction which may be the first instruction in a three guestinstruction sequence. As illustrated in FIG. 8, the first guestinstruction is dispatched by the emulation program of the previousinstruction, as described above. Thus, the first guest instructionemulation program includes instruction 1 on line 1 and instruction 2 online 2 which are found in the dispatch table. Instruction 2 on line 2causes a jump to line 3 of the program, found in the emulation programstore. These instructions for the MOVEM instruction will need a MOVEMopcode extension. This opcode extension will have been loaded by theprevious emulation program in the prefetch₋₋ data register, as itconsists of the next two bytes in the guest instruction sequence. Thus,an additional prefetch is needed to be executed to find the next guestinstruction. So, the pc value is then incremented by two bytes and aprefetch operation to fetch the instruction from the address pointed toby the register pc to a general purpose register gpr, other than theprefetch₋₋ data register is executed (line 4). The emulation programtests whether the second guest instruction, which is now indicated bythe value in the general purpose register is the expected second guestinstruction of the sequence and saves the result (line 5).

Line 6 of the program indicates that the extension data is used by theemulation program. An additional instruction or group of instructionsmay be executed (line 7). Lines 8 and 9 of the program illustrate thatthe DECODE1 and DECODE2 macros are executed. Line 10 indicates that aninstruction or instructions may be interspersed between the DECODE2macro and the PREFETCH macro on line 11. After the DECODE1, DECODE2 andPREFETCH macros are executed, a check for special conditions, such as aninterrupt or instruction tracing mode, is made (line 12). As indicatedat line 13, the program will branch if the sequence had been detected inline 5, and no special conditions were found. If the sequence is notdetected, then instructions indicated at line 14 are executed tocomplete emulation of the MOVEM instruction. Finally, the DISPATCH macrois executed to jump to the dispatch table entry for the second guestinstruction (line 15).

In FIG. 8, if the branch is taken at line 13, then the program moves toline 16. At this point, instructions may be executed. Line 17 of FIG. 8shows that the program then tests for the third expected guestinstruction in the sequence by looking at the value in the prefetch₋₋data register. As indicated at line 18, the algorithm will branch if thesequence is detected. If not, the instructions indicated by line 19 areexecuted to carry out a combined two instruction sequence. The DECODE1and DECODE2 macros are executed, as indicated at lines 20 and 21. Line22 indicates the possibility of interspersed instructions for theemulation program between the macros. At line 23, the PREFETCH macro isexecuted to prefetch the fourth guest instruction in the sequence. Next,the DISPATCH macro is executed to jump to the dispatch table entry forthe third guest instruction in the sequence (line 24).

If at line 18 of the program the branch was taken, then for the programillustrated in FIG. 8, line 25 is executed. This line indicates thatinstructions in the emulation program are executed. Line 26 illustratesthat an additional fetch operation is executed to retrieve the fourthguest instruction into the prefetch₋₋ data register. For the RTSinstruction in this sequence, the PREFETCH macro is replaced byinstructions to retrieve the target of the return from subroutineinstruction into the prefetch₋₋ data register.

Lines 27 and 28 correspond to the standard DECODE1 and DECODE2 macros inthe emulation system. Line 29 indicates the presence of interspersedinstructions to complete the combined three guest instruction emulation.Line 30 illustrates the presence of the PREFETCH macro to retrieve thefifth guest instruction in the sequence. Line 31 ends up the routinewith a DISPATCH macro which causes a jump to the dispatch table entry ofthe fourth guest instruction in the sequence.

Thus, it can be seen that the emulation program includes logic whichbypasses the dispatching of instructions in the detected sequence. Thisgreatly reduces the overhead involved in executing common guestinstruction sequences.

FIG. 9 illustrates an emulation program for a sequence of guestinstructions, such as a repeated sequence. As can be seen in FIG. 9, thefirst guest instruction is dispatched by the previous emulation program,and instructions 1 and 2 from the dispatch table entry are executed(lines 1 and 2). Instruction 2 causes a jump to the emulation programstore and execution of the sequence beginning with line 3.

Line 3 in FIG. 9 illustrates that the emulation program will includeinstructions that test the prefetch₋₋ data register for the sequence.This register will have been filled with the second guest instruction inthe sequence by the PREFETCH macro of the previous guest instruction.

As indicated on line 4, the algorithm branches if the sequence isdetected to line 11. If the sequence is not detected, then the algorithmcontinues with instructions indicated at line 5 to complete theemulation program. The emulation program would also include the DECODE1and DECODE2 macros as indicated at lines 6 and 7. Line 8 indicates thatinstructions of the emulation program may be interspersed with themacros involved in decoding and prefetching instructions. At line 9, thePREFETCH macro is executed to retrieve the third guest instruction inthe sequence. Line 10 is the DISPATCH macro which jumps to the dispatchtable entry for the second guest instruction. If at line 4, the branchwas taken, a PREFETCH macro is executed to retrieve the third guestinstruction. This macro is necessary because the DECODE1, DECODE2, andPREFETCH macros of lines 6, 7, and 9 were bypassed by the detection ofthe sequence of guest instructions. Then, the instructions at line 12are executed for the combined emulation program. Next, the emulationprogram will include a DECODE1 macro as indicated at line 13. Line 14indicates the possible distribution of instructions within the emulationprogram. Line 15 indicates the DECODE2 macro which loads the ctrregister with the address of the dispatch entry for the third guestinstruction. Line 16 indicates the PREFETCH macro for retrieving thefourth guest instruction in the sequence. Line 17 is the DISPATCH macrowhich causes a jump to the dispatch table entry for the third guestinstruction.

Thus, FIG. 9 illustrates the bypassing of decode and dispatch logic inthe emulation program for a common opcode sequence, such as a repeatedpair of opcodes.

FIG. 10 illustrates a program which is optimized for decoding repeatedsequences of instructions. In this example, the first instruction online 1 is a test for repeat and the second instruction is a jump fromthe dispatch table entry to an emulation routine on line 3 as shown inFIG. 10. Thus, lines 1 and 2 of FIG. 10 correspond to the dispatch tableentry for the first guest instruction.

At line 3, the routine executes an instruction or instructions relevantto the guest instruction being decoded. Next, the DECODE1 macro isexecuted, followed by the PREFETCH macro for the third guest instruction(lines 4 and 5). After the PREFETCH macro, an instruction orinstructions for emulating the guest instruction are executed (line 6).In line 7, the algorithm then branches if a repeat had been detected toline 11 of the routine. If no repeat had been detected, then the DECODE2macro is executed, as indicated at line 8. This macro is followed byinstructions indicated at line 9 which wrap up the emulated guestinstruction. Line 10 indicates the DISPATCH macro is executed. Thisresults in dispatching of the (2+N)th guest instruction, where "N" isthe number of times that a repeated instruction had been detected. Thus,if no repeat is detected at line 7, then the second guest instruction isdispatched at line 10.

If the branch had been taken at line 7, then the algorithm goes to line11 to test for a repeat once again. Thus, the second and third guestinstructions can be compared to determine whether a repeat has occurredat this point, because the PREFETCH macro on line 5 had prefetched thethird guest.

In line 12 of the routine, a branch is taken if a special condition isdetected to line 8 to complete the execution of the current guestinstruction. In line 13, additional instructions are executed to handlethe combined execution of repeated instructions. At line 14, the DECODE1macro is executed, followed by the PREFETCH macro in line 15. ThePREFETCH macro on line 15 prefetches the (3+N)th guest instruction,where "N" again is the value indicating the number of times that arepeat had been detected.

At line 16, instructions are executed relevant to emulation of the guestinstruction sequence. At line 17, the algorithm branches if a repeat hadbeen detected at line 11 back to line 11. The algorithm continues inthis loop from lines 11 through 17, until the repeated sequence ends.Line 18 of the routine causes a branch to line 8 to wrap up the sequenceif the branch at line 17 is not taken.

III. Details of a 68020 Emulation on a Power Architecture

The present invention may be further understood with reference to ordetailed information concerning emulation of the Motorola 68020microprocessor guest code on an IBM POWER microprocessor architecture.Thus, the internal design of a 68020 emulator for the POWER architectureprocessor is provided below.

POWER Registers

The POWER architecture defines 32 general purpose 32 bit registers(actually they can be 64 bits, but the emulator just uses the 32 bitarchitecture) referred to as r0 . . . r31. There are 32 double precision64 bit floating point registers referred to as f0 . . . f31, which arenot used at all by the emulator. There are 4 additional special purpose32 bit registers used by the emulator, they are called cr (conditionregister), xer (exception register), ctr (counter), and Ir (linkregister).

In the source code, the general purpose register are referred to bynames, instead of r0 . . . r31. These name will be used in the remainderof the document. The current assignments are as follows, although theycan easily be rearranged.

    ______________________________________                                        r0           zero                                                             r1           a7                                                               r2           (unused)                                                         r3           addr                                                             r4           data                                                             r5           rtn.sub.-- addr                                                  r6           immed.sub.-- data                                                r7           base.sub.-- disp (also called scaled.sub.-- index)               r8 . . . r15 d0 . . . d7                                                      r16 . . . r22                                                                              a0 . . . a6                                                      r23          (unused)                                                         r24          pc                                                               r25          sr.sub.-- and.sub.-- flags                                       r26          ccr.sub.-- x                                                     r27          prefetch.sub.-- data                                             r28          vbr                                                              r29          disp.sub.-- table                                                r30          code.sub.-- ptr                                                  r31          EmulatorStatePtr                                                 ______________________________________                                    

The zero register contains a constant value of zero, it is neverchanged. Assigning this to register r0 is also convenient due to thePOWER architecture base address quirk that does not allow r0 to be usedas a memory base register.

The register d0 . . . d7, a0 . . . a7, pc, sr₋₋ and₋₋ flags, ccr₋₋ x,and vbr are used to hold corresponding 68020 register state. This isdescribed in detail later in this document.

Registers addr, data, rtn₋₋ addr, immed₋₋ data, and base₋₋ disp are fivetemporary scratch registers used during the emulation of a 68020instruction. Although they can be used for many different purposes,their names describe how they are used by effective address calculationroutines, which are used during the emulation of many of the opcodes.

The prefetch₋₋ data register generally contains the sign extended 16 bitdata value pointed to by the pc register.

The disp₋₋ table register points to an entry in the 512KB instructiondispatch table. The opcode being dispatched to is inserted into thisregister to index the table, this is described in more detail later.

The code₋₋ ptr register points to the beginning of the 64KB block ofcode that contains the emulation routines this block is 64KB aligned sothat a 16 bit immediate value can be "or"ed with this register to pointto any address with this 64KB block of code.

EmulatorStatePtr points to the base of the memory area that is used tostore less frequently used emulator state that cannot be contained inthe POWER registers.

The POWER special purpose Ir register is available for use during theemulation of a 68020 instruction, and does not correspond to any 68020register state.

The POWER special purpose ctr register is available for use during theemulation of a 68020 instruction, and does not correspond to any 68020register state. It is used by convention to hold the address of thefirst POWER instruction to be executed to emulate the next 68020instruction.

The POWER special purpose xer register is used to hold the V and C bitsof the 68020 CCR register. It also contains the POWER SO bit, as well asthe byte count to be used by POWER string instructions.

The POWER condition register cr is used to hold the N and Z bits of the68020 CCR register. The low 16 bits are available for general use duringthe emulation of a 68020 instruction. The 4 bit condition registerfields cr1 and cr2 are not used, the 4 bits of cr3 are used for globalflags related to interrupts and special conditions which are describedlater.

68020 Register State Assignments

The 68020 has a number of general purpose and special purpose registers,some of which are only accessible in supervisor mode. All of theseregisters contents must be maintained by the emulator. In some cases,the bits in these registers may be distributed in a number of differentplaces within the POWER architecture, but the emulator willgather/scatter the bits whenever it encounters a 68020 instruction thataccesses the entire register. In other cases, there may be multiplecopies of a register contents. Many of the special purpose registers arestored in memory pointed to by EmulatorStatePtr. The 68020 registers andtheir POWER locations are as follows.

    ______________________________________                                        D0 . . . D7                                                                           d0 . . . d7                                                           A0 . . . A6                                                                           a0 . . . a6                                                           A7      a7                 (currently active                                                             stack pointer)                                     PC      pc                 (POWER version                                                                does not always                                                               point to executing                                                            instruction)                                       PC      trace.sub.-- pc(EmulatorStatePtr)                                                                (valid when tracing                                                           enabled)                                           CCR     cr/xer/ccr.sub.-- x                                                                              (bits are distributed)                             SR      sr.sub.-- and.sub.-- flags                                                                       (upper byte of SR                                                             only)                                              USP     saved.sub.-- usp(EmulatorStatePtr)                                                               (a7, when usp is                                                              active stack)                                      ISP     saved.sub.-- isp(EmulatorStatePtr)                                                               (a7, when isp is                                                              active stack)                                      MSP     saved.sub.-- msp(EmulatorStatePtr)                                                               (a7, when msp is                                                              active stack)                                      VBR     saved.sub.-- vbr(EmulatorStatePtr)                                                               (duplicate copy in                                                            vbr)                                               SFC     saved.sub.-- sfc(EmulatorStatePtr)                                    DFC     saved.sub.-- dfc(EmulatorStatePtr)                                    CACR    saved.sub.-- cacr(EmulatorStatePtr)                                   CAAR    saved.sub.-- caar(EmulatorStatePtr)                                   ______________________________________                                    

The 68020 registers d0 . . . d7/a0 . . . a6 are in POWER registers. The68020 has three stack pointers, and the active stack pointer will be inregister a7, while the remaining two inactive stack pointers will residein memory. The memory copy of the active stack pointer is not used andinconsistent while that stack pointer is selected as the active stackpointer. When the selection of the active stack pointer is changed, theregister copy of the old stack pointer will be written to memory, andthe new register copy of the active stack pointer will be read frommemory. It should be noted that register a7 is assigned to POWERregister r1, which is register used for the native POWER stack pointer.This is to allow a single stack model in a mixed emulated and nativeenvironment.

In the 68020, the pc generally points to the beginning of theinstruction that is currently being executed. During emulation the pcregister advances as the instruction is decoded and executed, andgenerally points somewhat past the beginning of the instruction beingexecuted. At the beginning of the execution of an instruction, the pcalways points two bytes (16 bits) past the beginning of the instruction,which may actually point to the next instruction. Since this offset isconstant, it is always possible to compute the actual pc at aninstruction boundary. When the 68020 instruction trace mode is active,the exception frame that is generated after the execution of a tracedinstruction needs to contain the pc of the beginning of the instructionthat has just completed. Since it is generally not possible to computethe size of the instruction that just completed, or worse yet, it mayhave been a branch instruction which computed a completely new pc, thereis a memory copy called trace₋₋ pc. When trace mode is active, the pc ofan instruction that is about to execute is save in the memory basedtrace₋₋ pc, so that the starting pc of the instruction can be determinedwhen the instruction completes. Since there is a performance penaltyassociated with this computation and updating, this is only performedwhen trace mode is enabled.

The 68020 CCR register consists of five condition code bits, called X,N, Z, V, and C. During emulation, these are treated as five separatebits which are in three different registers, instead of a single fieldof five bits. The X bit is stored in bit 2 of the POWER register namedccr₋₋ x. This bit position corresponds the position of the CA bit in thePOWER special purpose XER register. The N bit is stored in bit 0 of thePOWER cr register, this corresponds to the hi condition bit of cr0. TheZ bit is stored in bit 2 of the cr register, this corresponds to the EQcondition bit of cr0. The V bit is stored in bit 1 of the POWER specialpurpose XER register, which is the OV flag. The C bit is stored in bit 2of the XER register, which is the CA flag. Most of the 68020 datamovement and logical operations only update four of the five conditioncodes. They leave the X bit unchanged, set the N and 7 bits to indicateif the result is negative or zero, and always clear the V and C bits.Using this arrangement, a single POWER instruction can move data fromone register to another, and update some or all of these four bits ofthe CCR as follows.

    ______________________________________                                        ao.   dst,src,zero                                                                             ;# move data, update N,Z, clear V,C                          or.   dst,src,zero                                                                             ;# move data, update N,Z                                     ao    dst,src,zero                                                                             ;# move data, clear V,C                                      caxo  dst,src,zero                                                                             ;# move data, clear V                                        a     dst,src,zero                                                                             ;# move data, clear C                                        ______________________________________                                    

Most of the 68020 arithmetic and shift operations update the X bit tothe same value as the C bit. Since the C bit is in the XER register, asimple move from the XER register into the ccr₋₋ x register is all thatis required to update the X bit. It should be noted that the 68020 X andC bits are set to 1 if there is a borrow during subtraction, while thePOWER (and most other RISC processors) set the CA bit to 0 if there is aborrow during subtraction. This will require the CA bit to becomplemented before saving it as the X and C bits. The same inversion isneeded when the X bit is used as a borrow-in for the 68020 SUBXinstruction. By using the following instruction pair, it is possible toperform a subtraction followed by an addition, which will set the CAflag to correspond to the 68020 conventions.

    ______________________________________                                        sfo.     dst,src,dst ;# dst ← dst-src, update N,Z,V                      a        tmp,dst,src ;# update C, ignore result                               ______________________________________                                    

The upper byte of the 68020 SR register is stored in the low 8 bits (24. . . 31) of the sr₋₋ and₋₋ flags register. The high 16 bits (0 . . .15) of this register contain test-mode enable flags. Bit 20 is theflag₋₋ group₋₋ 1₋₋ active bit which indicates that a group 1 exception(Bus Error or Address Error) is being processed, and is used to detect aDouble Bus Fault situation. Bit 22 is the flag₋₋ odd₋₋ pc bit, which isused for Address Error detection. Bit 23 is the flag₋₋ trace₋₋ pendingbit, which indicates that the 68020 instruction currently being executedis being traced, and needs to generate a trace exception when itcompletes.

Many of the 68020 special purpose registers that are accessed via theMOVEC instruction are stored in memory, because they are infrequentlyaccessed, and there are not enough POWER registers available to hold allof them. An exception to this is the vbr register, there is a dedicatedPOWER register that is used to hold a copy of the vbr register contents,however the memory copy is also kept up to date. The various stackpointers are also an exception. Since only one of the three stackpointers can be selected at a time, the register a7 is used for theselected stack pointer, and the remaining two inactive stack pointersare stored in memory.

The Dispatch Table

The dispatch table is an indexed table of 65536 pairs of POWERinstructions, which correspond to the entry points for the routines toemulate each of the 65536 possible 68020 instruction encodings. Thefirst instruction of the pair will generally perform some operationrelated to source operand fetching or addressing. The second instructionof the pair is generally a branch instruction to an emulation routinewhich resides outside of this table. Since each pair of instructionswill occupy 8 bytes, the total size of this table is 512K bytes.Currently this table is located at addresses $68000000 . . . $6807FFFF,and can reside in Read Only Memory (ROM). The register disp₋₋ table isused to address this table.

The alignment of the beginning of the table is very important, it needsto start at either the beginning of a 2MB boundary, or 512KB past thebeginning. By having this 512KB block aligned to a 512KB address, thePOWER Block Address Translation (BAT) registers can be used to performthe address translation of this fairly randomly accessed block of code,and eliminate potential thrashing of the TLBs.

The 512KB alignment also allows a single POWER instruction to index thetable using the 68020 opcode times 8. The following instruction willshift the opcode left by 3 bits (multiply by 8), and insert it into theaddress of the base of the table, forming the address of the table entryfor that opcode.

    ______________________________________                                        rlimi        disp.sub.-- table,opcode,3,0x0007FFF8                            ______________________________________                                    

By having this table start in the first 1 MB of a 2 MB aligned boundary,where the second 1MB of addresses will cause an exception if accessed,it is possible to use an additional address bit to assist in thedetection of Address Errors (see discussion later in this document).

The Emulation Routines

There is a block of 64K bytes allocated for the POWER instructions theDispatch Table branches to. In general, the first two POWER instructionsin the emulation of a 68020 instruction reside in the Dispatch Table,while the remaining instructions, which we refer to as the Emulationroutines, reside in this block of code. Currently this block is locatedat addresses $68080000 . . . $6808FFFF, and can reside in Read OnlyMemory (ROM). The register code₋₋ ptr contains the address of thebeginning of this block.

Just like the Dispatch Table, the alignment of the block of code for theEmulation routines is also very important, it needs to start at thebeginning of a 64KB boundary.

In the emulator, it is frequently desirable to compute the address of aninstruction within this block of code. The POWER architecture does notprovide an instruction to compute a pc-relative address. The registercode₋₋ ptr points to the beginning of the block, and there is a label inthe source code called cb which marks the code base. To easily computethe address of any label within this 64 KB block of code, the followinginstruction can be used.

    ______________________________________                                        ori             addr,code.sub.-- ptr,label-cb                                 ______________________________________                                    

Within the 64 KB block of code, there is additional attention paid tocode alignment. The 601 processor cache has 32 byte cache blocks, and acache line consisting of 2 cache blocks, or 64 bytes. To improvelocality, and reduce the number of bytes of code that needs to befetched when there is a cache miss, the routines are packed into nicelyaligned 32 or 64 byte blocks.

68020 Instruction Prefetching

On the 601 processor, as well as most other RISC processors, there issome latency associated with memory read operations, and attempts to usethe results of a load instruction, in the very next instruction willusually result in a pipeline stall. To improve performance, and minimizethese stalls, it is very desirable to issue memory reads severalinstructions before attempting to use the data that they read.

Since the emulator needs to read all of the 68020 opcode and operandbytes in order to emulate an instruction, performance can be improved byissuing these reads long before the data is needed. To accomplish this,the emulator uses a register called prefetch₋₋ data to read (orpre-fetch) the next 16 bits (sign extended) of the instruction streaminto, as soon as the current 16 bits have been consumed. The register pcis used to point to the position within the 68020 instruction streamthat has been read. The POWER architecture provides an efficientinstruction that can both advance the pc register, and read the datapointed to by the updated pc. The instruction is as follows, and thereis also a macro called PREFETCH that is used within the emulator sourcecode.

    Ihau prefetch.sub.-- data,2(pc)

The prefetched data is always read 16 bits at a time, and sign extended,because the 68020 opcodes and extension words are most often organizedin 16 bit groups. The sign extension is useful for the addressing modesthat use a 16 bit signed displacement that is added to an A-register orthe PC register.

68020 Instruction Decoding

By using many of the concepts introduced above, the four basic stepsrequired to decode and emulate a simple 68020 instruction can now bedescribed. The four steps are referred to as DECODE1, DECODE2, PREFETCH,and DISPATCH. For simplicity, we will assume that this is a 16 bitopcode that does not perform any useful operation.

Since this is a very pipelined sequence of events, and we must startsomewhere in the pipeline, we will begin at the first instruction in thedispatch table for this opcode, and after going through the remainingstages, we will see how we get back here after completing the remainingphases.

Upon entry, the following registers are setup as follows. The disp₋₋table and ctr registers contains the address of the dispatch table entryfor this opcode (the POWER address that we are currently executing at).The pc register points 2 bytes past the 68020 opcode that we are aboutto emulate. The prefetch₋₋ data register contains the sign extended 16bit value that the pc register points to (in this example, it is thenext 68020 opcode to be emulated).

The first phase is DECODE1, this phase is begins the decoding of thenext 68020 instruction to be emulated. In this example, we are assumingthat the current 68020 instruction consists of just a 16 bit opcode, anddoes not have any extension words. If there were extension words, theywould need to be consumed by PREFETCHing, until prefetch₋₋ data containsthe opcode of the next 68020 instruction, and pc points to that opcode.The DECODE1 operation takes the next 68020 opcode that is in theprefetch₋₋ data register, multiplies it by 8, and inserts it into thedisp₋₋ table register, forming the address of the dispatch table entryfor the next 68020 instruction. This is done in a single POWERinstruction as follows, the macro DECODE1 performs this instruction.

    ______________________________________                                        rlimi      disp.sub.-- table,prefetch.sub.-- data,3,0x0007FFF8                ______________________________________                                    

Since DECODE1 was the first of the two POWER instructions that reside inthe dispatch table entry for this 68020 instruction, the secondinstruction must be a branch out of the dispatch table, and into anemulation routine. This is not considered to be one of the phases of thedecoding process, but rather a necessity imposed by the two instructionlimit with a dispatch table entry. In this example we will assume thatthis branch is as follows.

    ______________________________________                                                b           continue                                                  ______________________________________                                    

The second phase is DECODE2, which in this example will occur in thefirst POWER instruction of the emulation routine. DECODE2 simply takesthe dispatch table entry address that was computed by DECODE1, and movesit into the POWER ctr register. This is because the POWER branchinstructions cannot branch to addresses contained in the general purposeregisters, and can only branch to addresses in either the ctr or Irspecial purpose registers. The DECODE2 phase is done in a single POWERinstruction as follows, the macro DECODE2 performs this instruction.

    ______________________________________                                        continue:                                                                            mtctr        disp.sub.-- table                                         ______________________________________                                    

The third phase is PREFETCH, which in this example will occur in thesecond POWER instruction of the emulation routine. As described earlier,PREFETCH will advance the pc register by 2 bytes, and read the signextended 16 bit value at that address into the prefetch₋₋ data register.We need to prefetch at this time, because we have consumed the previouscontents of the prefetch₋₋ data register, which had contained the 16 bit68020 opcode for next instruction to be emulated. This will setup theprefetch₋₋ data register with the first extension word (if any)associated with the next opcode, or the opcode of the instructionfollowing the next instruction. As shown earlier, the PREFETCH phase isdone in a single POWER instruction as follows, the macro PREFETCHperforms this instruction.

    ______________________________________                                        lhau             prefetch.sub.-- data,2(pc)                                   ______________________________________                                    

The fourth and final phase is DISPATCH, which in this example will occurin the third and fourth POWER instructions of the emulation routine.There are two POWER instructions needed for this phase, but in generalthe second one never gets executed. The DISPATCH phase completes theemulation of the current 68020 instruction, and begins the emulation ofthe next 68020 instruction. Since this marks the boundary between two68020 instructions, any special processing that needs to occur betweentwo 68020 instructions must happen here. Instruction Trace exceptions,and 68020 interrupt processing are examples of special events that needto be processed on instruction boundaries. There is a bit in the POWERcr register referred to as cr₋₋ special₋₋ event₋₋ pending, which getsset whenever any of this special handling is needed. How this bit getsset will be described later, but for now, lets just assume that it iscleared. Since the dispatch table entry address for the next 68020instruction is already loaded into the POWER otr register, the DISPATCHphase simply needs to branch to this address when there are no specialevents pending, or branch to a common routine to process pending specialevents. This final phase is done in two POWER instructions as follows,the macro DISPATCH performs these instructions.

    ______________________________________                                        bfc            cr.sub.-- special.sub.-- event.sub.-- pending                  b              process.sub.-- special.sub.-- event                            ______________________________________                                    

By breaking the decoding and dispatching process into these simplephases, the instructions to perform the various phases can bedistributed between other instructions within the emulation routines toexecute in gaps where the processor would have otherwise stalled due tomemory access latency.

Effective Address Computation

The 68020 architecture has a number of different addressing modes, someare very simple, and some can become very complex. Since the effectiveaddress, and the data that it points to, is generally needed very earlyin the emulation of a 68020 instruction, it is convenient to have anumber of Effective Address Computation routines that can run beselected based upon the addressing mode, and a common emulation routineto implement the operation independent of the addressing mode used.

In some cases, the entire Effective Address Computation can occur in asingle POWER instruction, and can be placed in the Dispatch Table. Inother cases a short (or possibly long) subroutine is needed. Since thereis only room for two instructions in the dispatch table, one method ofperforming a subroutine call would be to have the first instructioncontain a call to the subroutine, which would return to the secondinstruction, which would be a branch to the emulation routine. Thiswould result in a branch to a branch instruction, on the initialdispatch, and a branch to a branch instruction when returning from theEffective Address subroutine. This type of branching does not performwell on the POWER processor. It is more desirable to have the EffectiveAddress calculation subroutine return directly to the @ firstinstruction of the emulation routine.

To reduce the number of branches, a slightly different subroutinecalling convention is used. The first instruction in the dispatch tablewill load the address of the emulation routine into the register rtn₋₋addr, and the second instruction will branch to the Effective Addresssubroutine. The subroutine will move the address of the emulationroutine from rtn₋₋ addr into the Ir, and when it returns, it will returnto the emulation routine.

routine, the code₋₋ ptr register is used as a base address. An exampleof what the two instructions in the dispatch table may look like is asfollows.

    ______________________________________                                        ori         rtn.sub.-- addr,code.sub.-- ptr,not.sub.-- l.sub.-- mem-cb        b           cea.sub.-- l.sub.-- 30                                            ______________________________________                                    

There are some register conventions used by the effective addressroutines. As mentioned before rtn₋₋ addr is used to pass the returnaddress, and may be used as a scratch register by the Effective Addressroutine. The register addr is used to return the effective address (ifone is computed). The register data will contain the data that was readfrom the effective address (if it was a Fetch Effective Address routine), or will remain unchanged. The register immed₋₋ data is used to returnthe immediate operand, or opcode extension word that follows to opcodebut precedes the Effective Address extension words, forImmediate/Extended Effective Address routines. The register base₋₋ dispis used as a scratch register.

The 68020 modes 6n and 73 indexed addressing modes can be very complex.In it's simplest form, any of the sixteen 68020 A/D registers can beused as an index. The index can be treated as a sign extended 16 bitquantity, or a full 32 bit value, and can be multiplied by 1, 2, 4, or8, added to a base register, and added to a sign extended 8 bitdisplacement. Most of the information needed to compute the address isin a secondary extension word. There is an additional bit that indicatesthat even more complex addressing options are available. To quicklydecode all of these options, there is a 256 entry table of instructionpairs that is at the beginning of the emulation routine code block. Thecode₋₋ ptr register points to the base of this indexed addressing modedecode table.

Address Error Processing

Since all instructions in the 68020 architecture are a multiple of twobytes long, the 68K does not allow instructions to begin on oddaddresses. If a branch to an odd address is attempted, an Address Errorexception is generated. Since this exception is an indication of aprogramming error, and is not a normal occurrence, the emulator wouldlike to spend as little time as possible checking for this condition.There have been two methods used in the emulator to detect branches toodd addresses, each has different performance characteristics.

The first method (which is no longer used) consists of two phases,called CHECK₋₋ ODD₋₋ PC1 and CHECK₋₋ ODD₋₋ PC2. Each phase consisted ofa single instruction. The first instruction would move the low bit ofthe pc register into bit 31 of the cr register. The second instructionwould "or" bit 31 into the special event pending flag. This would causethe process₋₋ special₋₋ event routine to be entered if the new pc valuewas odd. That routine would check to see if the pc was odd, and generatean Address Error exception if it was. The POWER instructions used by thetwo phases are as follows.

    ______________________________________                                        mtcrf 0x01,pc                                                                 cror  cr.sub.-- special.sub.-- event.sub.-- pending,cr.sub.-- special.sub.          -- event.sub.-- pending,31                                              ______________________________________                                    

The second method consists of a single phase, called CHECK₋₋ ODD₋₋ PC1,which is a single POWER instruction. This instruction will insert thelow bit of the pc register into bit 11 of the disp₋₋ table register.Assuming that this is done before the DECODE2 phase, it will cause theDISPATCH phase to jump to an address that is 1MB past the Dispatch TableEntry that should have been used. This is an illegal address, and willcause an exception when the DISPATCH is attempted. The handler for thisexception will notice that the bit in disp₋₋ table had been set, andwill cause an Address Error exception to be generated. This method hasless overhead than the first method when the pc is even, butsignificantly more overhead when the pc is odd. The single POWER used bythis method is as follows.

    ______________________________________                                        rlimi         disp.sub.-- table,pc,20,0x00100000                              ______________________________________                                    

MOVEM Register List Optimizations

The 68020 MOVEM instruction has a 16 bit register list mask associatedwith it. There is one bit for each of the 16 A/D registers, indicatingif the register should be moved. In general, the emulator would need toperform 16 individual tests of the bits to determine if thecorresponding register needed to be read/written. This can be very timeconsuming. Due to compiler register allocation, parameter passing, andcalling conventions, there are some registers that rarely appear in theregister list of the MOVEM instruction, and others that are much morefrequent. Taking advantage of this, the emulator can first check to seeif any of the infrequent registers are on the list, using a bitwise"and" operation against the mask. If none of those bits were set, thenthere is a much smaller list of bits that need to be checkedindividually (7 instead of 16), which will improve performance. Theinfrequent registers that the emulator currently test for ared0-d3/a0-a1/a5-a7 and the frequent registers are d4-d7/a2-a4.

Optimizations based on Opcods Synonyms

In the 68020 instruction set, there are many cases where two differentopcode encodings perform exactly the same operation. Since the emulatorcan uniquely decode each of the 65536 possible opcode encodings, it canmake sure that the dispatch table entries for two opcode synonyms arethe same, and have exactly the same performance. Below is a list ofinstructions and their synonyms.

    ______________________________________                                        add.<b,w,l>                                                                             #imm,dn  ≈                                                                           addi.<b,w,l>                                                                            #imm,dn                                    adda.w    #imm,an  ≈                                                                           lea.l     imm(an),an                                 addq.w    #imm,an  ≈                                                                           addq.l    #imm,an                                    and.<b,w,l>                                                                             #imm,dn  ≈                                                                           andi.<b,w,l>                                                                            #imm,dn                                    asl.<b,w,l>                                                                             #1,dn    ≈                                                                           add.<b,w,l>                                                                             dn,dn                                      bra.s     *+4      ≈                                                                           dbt.w     dx,xxxx                                                                       (32 bit nop)                               bra.w     d16      ≈                                                                           jmp       d16(pc)                                    bsr.w     d16      ≈                                                                           jsr       d16(pc)                                    clr.l     dn       ≈                                                                           moveq.l   #0,dn                                      cmp.<b,w,l>                                                                             #imm,dn  ≈                                                                           cmpi.<b,w,l>                                                                            #imm,dn                                    lea.l     (as),ad  ≈                                                                           movea.l   as,ad                                      lea.l     abs.w,an ≈                                                                           movea.w   #imm16,an                                  lea.l     abs.l,an ≈                                                                           movea.l   #imm32,an                                  movea.l   (a7),a7  ≈                                                                           unlk      a7                                         or.<b,w,l>                                                                              #imm,dn  ≈                                                                           ori.<b,w,l>                                                                             #imm,dn                                    sub.<b,w,l>                                                                             #imm,dn  ≈                                                                           subi.<b,w,l>                                                                            #imm,dn                                    subq.w    #imm,an  ≈                                                                           subq.l    #imm,an                                    ______________________________________                                    

Optimizations based on Operands

In many cases, opcodes that have the same register specified as both thesource and destination behave the same as some other Opcode whichexecutes faster. For these opcodes, we create an optimized dispatchtable entry which is the same as the simpler opcode, instead of usingthe dispatch table entry for the general case. The table below shows thetransformations.

    ______________________________________                                        and.<b,w,l>                                                                             dn,dn    →                                                                            tst.<b,w,l>                                                                             dn                                         cmp.<b,w,l>                                                                             dn,dn    →                                                                            tst.l     zero                                       cmpa.l    an,an    →                                                                            tst.l     zero                                       eor.<b,w,l>                                                                             dn,dn    →                                                                            clr.<b,w,l>                                                                             dn                                         exg.l     rn,rn    →                                                                            nop                                                  lea.l     (an),an  →                                                                            nop                                                  move.<b,w,l>                                                                            dn,dn    →                                                                            tst.<b,w,l>                                                                             dn                                         movea.l   an,an    →                                                                            nop                                                  movea.<w,l>                                                                             (an)+,an →                                                                            movea.<w,l>                                                                             (an),an                                    or.<b,w,l>                                                                              dn,dn    →                                                                            tst.<b,w,l>                                                                             dn                                         sub.<b,w,l>                                                                             dn,dn    →                                                                            clr.<b,w,l>                                                                             dn (also clear                                                                ccr.x)                                     ______________________________________                                    

In many cases, memory to memory MOVE instructions that have the samesource and destination addresses behave the same as a TST instruction.If we can assume that the read and the write will be to the same RAMlocation, and that there are no side effects (write protected or I/Ospace accesses), then it should be safe to omit the write, and just dothe read. The table below shows the transformations.

    ______________________________________                                        move.<b,w,l>                                                                            (an),(an)  →                                                                            tst.<b,w,l>                                                                            (an)                                      move.<b,w,l>                                                                            -(an),(an) →                                                                            tst.<b,w,l>                                                                            -(an)                                     move.<b,w,l>                                                                            (an)+,-(an)                                                                              →                                                                            tst.<b,w,l>                                                                            (an)                                      move.<b,w,l>                                                                            -(an),(an)+                                                                              →                                                                            tst.<b,w,l>                                                                            <1,2,4>(an)                               ______________________________________                                    

Optimizations based on repeated Opcodes

There are many places in the Macintosh QuickDraw routines where datamovement loops are "unwound" and contain 16 repeated sequences of thesame instruction, followed by a DBRA looping instruction. There are alsocases of shorter sequences in compiler generated code for structurecopying. Since the emulation of a repeated sequence of 68020 opcodeswill cause a repeated sequence of POWER instructions to be executedwithin the emulator, it is possible for the emulation of one of theseopcodes to detect that the next opcode is the same, and eliminate someof the decoding and dispatching overhead, which will improve theperformance of the subsequent instances of the same opcode. Ifinstruction tracing or an interrupt is pending, this optimization cannotbe performed, because special event processing would need to occur atthe instruction boundaries. The opcodes that the emulator currentlydetects repeated sequence of are shown below.

    ______________________________________                                               move.l      d6,(a1)+                                                          move.l      d6,(a5)+                                                          eor.l       d6,(a5)+                                                          move.l      (a0)+,(a1)+                                                       move.l      (a0)+,(a2)+                                                       move.l      (a1)+,(a0)+                                                       move.l      (a2)+,(a1)+                                                       move.l      (a4)+,(a5)+                                                ______________________________________                                    

Optimizations based on Opcode Sequences

In compiler generated code, and sometimes in the Macintosh ROM codethere are common sequences of two and sometimes three instructions thatoccur frequently due to runtime calling conventions. In many casesdetecting these sequences is done by having the emulation of the firstinstruction of the sequence check to see if it is followed by the secondinstruction of the sequence. For sequences of three instructions, if thepair of the first two had been detected, then the check for the thirdinstruction can be made. The emulator currently detects and optimizesthe following pairs and triples of instructions. As with otheroptimizations of this kind, the optimization cannot be performed ifspecial events are pending. The sequences that the emulator may detectare shown below.

    ______________________________________                                        move.b (a1)+,(a0)+         bne.s   *-2                                        move.b (a0)+,d0            cmp.b   (a1)+,d0                                   move.b (a0)+,d1            cmp.b   (a1)+,d1                                   move.l (a7)+,(a7)          rts                                                move.l ([d16,ZA0,ZA0.w*1],d16),-(a7)                                                                     rts                                                move.l abs.w,-(a7)         rts                                                movem.l                                                                              d16(a6),reg.sub.-- list                                                                           unlk    a6    rts                                  movem.l                                                                              (a7)+,reg.sub.-- list                                                                             unlk    a6    rts                                  ______________________________________                                    

ATrap Dispatcher Acceleration

The Macintosh OS and Toolbox use the unimplemented LineA opcode space toencode system calls. The LineA dispatcher in the ROM executes severalinstructions (on every call) to dispatch to the desired routine. Thiswhole process can be greatly improved by having the emulator directlydispatch these instructions. However, there must also be a way for thisfeature to be disabled if the standard LineA dispatcher has beenreplaced, such as when using the A-Trap record/break features ofMacsBug. In order to achieve this compatibility, we need to know if thehandler address (in vector offset $28) has changed. Fortunately, we donot have to worry about tracing, since the 68000 will not take a tracetrap on a LineA (or any other unimplemented) instruction.

IV. Conclusion

This emulation technique is also optimized for an upgraded version ofthe POWER architecture, referred to as the POWER PC architecture. Thisupdated version of the POWER architecture executes the same opcodeslisted above for the POWER architecture instructions, except withdifferent mnemonics.

Thus, a highly efficient method for decoding 68020 instructions in thePOWER architecture processor, utilizing a special table of instructionsequences, specific sequences of instructions and other optimizations isprovided. The alignment and method of indexing the table contribute tothe high speed decoding of the 68020 instructions. In particular, asingle POWER instruction can be used to index the table, by aligning thedispatch table on a 512K byte boundary within a two megabyte range,shifting the opcode left by three bits and inserting the shifted opcodeinto the address of the base of the table. Each entry in the tablecontains two POWER instructions. The first instruction generallyperforms an operation specific to source addressing mode, and the secondinstruction branches to an emulation routine which is generallyindependent of the source addressing mode. This allows reduced codesize, and better memory reference and cache locality for the emulationroutines.

There are four phases of the instruction decoding process. DECODE1 formsthe address of the next dispatch table entry by shifting and insertingthe next 68020 opcode into the address of the base of the dispatchtable. DECODE2 moves that dispatch table address to the powerarchitecture CTR register. PREFETCH reads the 16 bits (assigned extendedto 32 bits) that follow the next 68020 instruction into a temporaryholding register called prefetch₋₋ data. Finally, DISPATCH jumps to theaddress that was computed by DECODE1.

This sequence of four instruction macros is the minimum number ofinstructions in this embodiment of the invention that this task can beperformed in on the POWER architecture, and the use of opcodeprefetching reduces or eliminates pipeline stalls due to memoryreference latency.

Overall, performance of an emulating guest instructions is enhanced byreducing the number of host instructions needed to be executed toemulate the guest instruction. One primary reason for this benefit isthe direct access to the instructions in the dispatch table, combinedwith prefetching of the guest instructions. Furthermore, opcodesequences of any length may be optimized, and decoding and dispatchlogic bypassed for the sequence, to further enhance emulatorperformance.

The foregoing description of preferred embodiments of the presentinvention has been provided for the purposes of illustration anddescription. It is not intended to be exhaustive or to limit theinvention to the precise forms disclosed. Obviously, many modificationsand variations will be apparent to practitioners skilled in this art.The embodiments were chosen and described in order to best explain theprinciples of the invention and its practical application, therebyenabling others skilled in the art to understand the invention forvarious embodiments and with various modifications as are suited to theparticular use contemplated. It is intended that the scope of theinvention be defined by the following claims and their equivalents.

What is claimed is:
 1. A computer system that decodes guest instructionsfor emulation, comprising:host processor that executes hostinstructions; guest program store accessible by said host processor,said guest program store containing a program sequence of said guestinstructions for emulation by said host processor; emulation routinestore accessible by said host processor, said emulation routine storecontaining a set of emulation routines each corresponding to aparticular one of said quest instructions, said emulation routines forexecution by said host processor during emulation of said programsequence, each emulation routine having dispatch logic for handling anext guest instruction after said particular one of said guestinstructions in said program sequence wherein said dispatch logichandles said next guest instruction by initially determining whethersaid particular one of said guest instructions followed by said nextguest instruction correspond to a predefined opcode sequence, saiddispatch logic then emulating said next guest instruction for saidpredefined opcode sequence without dispatching to said emulation routinethat corresponds to said next guest instruction if said particular oneof said guest instructions followed by said next guest instructioncorrespond to said predefined opcode sequence, and wherein said dispatchlogic handles said next guest instruction by dipatching to saidemulation routine that corresponds to said next guest instruction ifsaid particular one of said guest instructions followed by said nextguest instruction do not correspond to said predefined opcode sequence.2. The computer system of claim 1, wherein said dispatch logic detectssaid predefined opcode sequence by prefetching said next guestinstruction in said program sequence and determining whether saidparticular one of said guest instructions emulated by said emulationroutine that corresponds to said particular one of said guestinstructions followed by said next guest instruction correspond to saidpredefined opcode sequence.
 3. The computer system of 2, wherein saiddispatch logic also includes logic for determining whether an opcode ofsaid next guest instruction is the same as an opcode of said particularone of said guest instructions emulated by said emulation routine andthen emulates said next guest instruction if said opcodes are the samewithout dispatching to said emulation routine that corresponds to saidnext guest instruction.
 4. The computer system of claim 3, wherein saiddispatch logic dispatches said next guest instruction to said emulationroutine for said next guest instruction contained in said emulationroutine store if said opcodes are not the same.
 5. The computer systemof claim 4, further comprising a dispatch table store accessible by thehost processor, said dispatch table store having a plurality of entriesincluding an entry corresponding to said particular guest instruction ofsaid guest instructions wherein said entry corresponding to saidparticular guest instruction contains a set of host instructions forexecution by said host processor during emulation of said particularguest instruction.
 6. The computer system of claim 5, wherein saidemulation logic dispatches said next guest instruction by determining anindex into said dispatch table store in response to said next guestinstruction by converting said next guest instruction into said indexwithin said host processor and by directly accessing said hostinstructions contained in said dispatch table that correspond to saidnext guest instruction according to said index.
 7. The computer systemof claim 6, wherein said emulation logic determines said index into saiddispatch table store by providing an instruction that loads aspecialized address register in said host processor with said next guestinstruction multiplied by a constant value.
 8. The computer system ofclaim 7, wherein said instruction that loads said specialized addressregister shifts said next guest instruction at least one bit positionaccording to said constant value.
 9. The computer system of claim 8,wherein said instruction that loads said specialized address register insaid host processor obtains said next guest instruction from aspecialized prefetch register in said host processor.
 10. A method fordecoding guest instructions for emulation in a computer system having ahost processor that executes host instructions, said method comprisingthe steps of:providing a guest program store accessible by said hostprocessor that contains a program sequence of said guest instructionsfor emulation by said host processor; providing an emulation routinestore accessible by said host processor that contains a set of emulationroutines for execution by said host processor during emulation of saidguest instructions wherein each emulation routine corresponds to aparticular one guest instruction of said guest instructions, whereineach emulation routine when executed by said host processor handles anext guest instruction after said particular one of said guestinstructions in said program sequence by performing the steps ofinitially determining whether said particular one of said guestinstructions followed by said next guest instruction correspond to apredefined opcode sequence, then emulating said next guest instructionfor said predefined opcode sequence without dispatching to saidemulation routine that corresponds to said next guest instruction ifsaid particular one of said guest instructions followed by said nextguest instruction correspond to said predefined opcode sequence, andotherwise by performing the step of dispatching to said emulationroutine that corresponds to said next guest instruction if saidparticular one of said guest instructions followed by said next guestinstruction do not correspond to said predefined opcode sequence. 11.The method of claim 10, wherein said step of determining whether saidparticular one of said guest instructions followed by said next guestinstruction correspond to a predefined opcode sequence includes the stepof prefetching said next guest instruction in said program sequence. 12.The method of 11, wherein each emulation routine when executed by saidhost processor handles said next guest instruction by performing thesteps of determining whether an opcode of said next guest instruction isthe same as an opcode of said particular guest instruction emulated bysaid emulation routine and then emulating said next guest instruction ifsaid opcodes are the same.
 13. The method of claim 12, wherein eachemulation routine when executed by said host processor handles said nextguest instruction by further performing the step of dispatching saidnext guest instruction to said emulation routine for said next guestinstruction if said opcodes are not the same.
 14. The method of claim13, further comprising the step of providing a dispatch table storeaccessible by the host processor, said dispatch table store having aplurality of entries including an entry corresponding to said particularguest instruction of said guest instructions wherein said entrycorresponding to said particular guest instruction contains a set ofhost instructions for execution by said host processor during emulationof said particular guest instruction.
 15. The method of claim 14,wherein said step of dispatching said next guest instruction includesthe step of determining an index into said dispatch table store inresponse to said next guest instruction by converting said next guestinstruction into said index within said host processor and by directlyaccessing said host instructions contained in said dispatch table thatcorrespond to said next guest instruction according to said index. 16.The method of claim 15, wherein said step of determining said index intosaid dispatch table store includes the step of loading a specializedaddress register in said host processor with said next guest instructionmultiplied by a constant value.
 17. The method of claim 16, wherein saidstep of loading said specialized address register includes the step ofshifting said next guest instruction at least one bit position accordingto said constant value.
 18. The method of claim 17, wherein said step ofloading said specialized address register in said host processorincludes the step of obtaining said next guest instruction from aspecialized prefetch register in said host processor.